/*
 * Daniel R Padilla
 *
 * Copyright (c) 2007, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.table.column;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.databasefacade.translator.Translator;


/**
 * Array list that holds a collection
 * of columns.
 * 
 * @author Daniel
 *
 */
public class AliasedColumnList
{
	private ArrayList aliasedColumnList = new ArrayList();
	private HashMap aliasedColumnHash = new HashMap();
	private Translator translator;
	
	public AliasedColumnList(Translator translator) {
		this.translator =  translator;
	}
	
	public void addAliasedColumn(Column col)
	{
		add(col.getTableAlias(), col, col.getColumnName());
	}
	
	public void addAliasedColumn(Column col, String colAlias)
	{
		add(col.getTableAlias(), col, colAlias);
	}
	
	public void removeAliasedColumn(int i)
	{
		String alias = (String) aliasedColumnList.get(i);
		aliasedColumnList.remove(i);
		aliasedColumnHash.remove(alias);
	}
	
	public void removeAliasedColumn(String alias)
	{ 
		for ( int i = 0; i < aliasedColumnList.size(); i++ ) {
			String lAlias = (String) aliasedColumnList.get(i);
			if ( alias.equals(lAlias) ) {
				removeAliasedColumn(i);
				break;
			}
		}
	}
	
	public AliasedColumn getAliasedColumn(int i) {
		String alias = (String) aliasedColumnList.get(i);
		return getAliasedColumn(alias);
	}
	
	public Column getColumn(int i)
	{
		return (Column) getAliasedColumn(i).getColumn();
	}
	
	public AliasedColumn getAliasedColumn(String alias)
	{
		return (AliasedColumn) aliasedColumnHash.get(alias);
	}
	
	public Column getColumn(String alias)
	{
		AliasedColumn acol = getAliasedColumn(alias);
		Column col = acol.getColumn();
		return (Column) col;
	}
	
	public int size()
	{
		return aliasedColumnList.size();
	}
	
	public Iterator getColumnAliases()
	{
		return aliasedColumnList.iterator();
	}
		
	/**
	 * Return the comma delimited aliased fields
	 * 
	 * @return
	 */
	public String getSQL()
	{
		String sql=null;
		for(int i=0; i < aliasedColumnList.size(); i++) {
			AliasedColumn aliasedCol = getAliasedColumn(i);
			if ( sql == null ) {
				sql = aliasedCol.getSQL();
			} else {
				sql += "\n      ," + aliasedCol.getSQL();
			}
		}
		return sql;
	}


	public void clearColumns()
	{
		aliasedColumnList.clear();
		aliasedColumnHash.clear();
	}
	
	private void add(String tableAlias, Column col, String colAlias)
	{
		AliasedColumn alias = new AliasedColumn(translator, col, tableAlias, colAlias);
		if ( !aliasedColumnList.contains(colAlias) ) {
			aliasedColumnList.add(colAlias);
		}
		aliasedColumnHash.put(colAlias, alias);
	}
}
